/**
*      author: Y_Yao
*      created: 2024-10-22 18:23:24
**/

#include<bits/stdc++.h>

using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 1000 + 7;
int a[N];
int n,m;

int Find(int i)
{
    if(a[i] == i) return i;
    else a[i] = Find(a[i]);

    return a[i];
}

void Union(int t1,int t2)
{
    int root1 = Find(t1);
    int root2 = Find(t2);

    a[root1] = root2;
}

void solve()
{
    cin >> n >> m;
    for(int i = 1;i <= n;i++)
    {
        a[i] = i;
    }

    for(int i = 1;i <= m;i++)
    {
        int t1,t2;
        cin >> t1 >> t2;
        Union(t1,t2);
    }

    int ret = 0;
    for(int i = 1;i <= n;i++)
    {
        if(a[i] == i) ret++;
    }
    cout << ret << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 0;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}